package mamikeji.liaowa.ui.fragment.main_todo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.MainBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.MainActivity;
import mamikeji.liaowa.ui.fragment.BaseFragment;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;

/**
 * Created by 廉冲-android开发 on 2017/8/5.
 */
public class ToDo_SeekBarFragment extends BaseFragment {
    private static final String TAG = "ToDo_SeekBarFragment";
    View mView;
    @BindView(R.id.textView_todo_seek_title)
    TextView textViewTodoSeekTitle;
    @BindView(R.id.seekBar_min)
    TextView seekBarMin;
    @BindView(R.id.seekBar_value)
    TextView seekBarValue;
    @BindView(R.id.seekBar_max)
    TextView seekBarMax;
    @BindView(R.id.seekBar_todo)
    SeekBar seekBarTodo;

    MainBean.DynamicEntity.TaskconfigEntity listEntity;
    @BindView(R.id.textView_todo_s_index)
    TextView textViewTodoSIndex;
    @BindView(R.id.textView_todo_s_max)
    TextView textViewTodoSMax;
    private int num , max;

    TextView[] textViews;

    int index_num = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_todo_seekbar, container, false);
        ButterKnife.bind(this, mView);
        getBase();

        listEntity = (MainBean.DynamicEntity.TaskconfigEntity) getArguments().getSerializable("todo");
        num = getArguments().getInt("todo_vp_num");
        max = getArguments().getInt("todo_vp_max");

        textViews = new TextView[]{seekBarMin, seekBarValue, seekBarMax};

        if (listEntity != null) {
            init();
        }
        return mView;
    }

    public static final ToDo_SeekBarFragment Instance(Serializable serializable, int num , int max) {
        ToDo_SeekBarFragment toDo_seekBarFragment = new ToDo_SeekBarFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("todo", serializable);
        bundle.putInt("todo_vp_num", num);
        bundle.putInt("todo_vp_max", max);
        toDo_seekBarFragment.setArguments(bundle);
        return toDo_seekBarFragment;
    }

    private void init() {
        textViewTodoSIndex.setText(""+(num+1));
        textViewTodoSMax.setText(""+max);
        textViewTodoSeekTitle.setText(listEntity.getTitle());
        final List<MainBean.DynamicEntity.TaskconfigEntity.AnswersEntity> childList = listEntity.getAnswers();
        if (childList != null && childList.size() > 0) {
            seekBarTodo.setMax(childList.size() - 1);
            int selectId = -1;
            for (int i = 0; i < childList.size(); i++) {
                textViews[i].setText(childList.get(i).getAnswertitle());
                if (childList.get(i).getIschoice() == 1) {
                    selectId = i;
                    index_num = i;
                }
            }

            if (selectId == -1) {
                seekBarTodo.setProgress(0);
            } else {
                seekBarTodo.setProgress(selectId);
            }

            seekBarTodo.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    updateToDoData(i);
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
        }
    }

    public void updateToDoData(final int position) {
        MainBean.DynamicEntity.TaskconfigEntity.AnswersEntity childBean = listEntity.getAnswers().get(position);
        Map<String, String> formMap = new HashMap<>();
        formMap.put("userid", ForPreference.getString(ForContents.USER_CODE));
        formMap.put("tid", listEntity.getTimuid() + "");
        formMap.put("timeid", ForPreference.getString(ForContents.WA_timeCode));
        formMap.put("oid", childBean.getAnswerid() + "");
        formMap.put("scoreLv", childBean.getScoresection() + "");
        formMap.put("score", listEntity.getScore() + "");
        formMap.put("ttype", "1");
        formMap.put("config_id", listEntity.getConfigid());
        formMap.put("category", listEntity.getCategory() + "");
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.TODO_ADD, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG + "updateToDoData", "" + e.getMessage());
                ForToast.showToast(R.string.intent_error);
                seekBarTodo.setProgress(index_num);
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        index_num = position;
                        seekBarTodo.setProgress(index_num);

                        String requestid = jsonObject.getString("requestid");
                        ((MainActivity)getActivity()).getToDo_vp(Integer.parseInt(requestid), true);
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
